import { baseUrl } from './request'
import { getTokenValue } from './storage'

let controller = null
export async function qaChat(message) {
  const content = {
    terminalType: 'h5',
    chatType: 'chat',
    model: 'zpGLM',
    direction: 'O',
    contextMsg: message,
  }

  // eslint-disable-next-line no-useless-catch
  try {
    const url = baseUrl + '/eims/BPai/chat'
    controller = wx.request({
      url,
      method: 'POST',
      data: JSON.stringify(content),
      header: {
        'Content-Type': 'application/json',
        Authorization: getTokenValue() || '',
      },
      success(res) {
        console.log('res.data')
        return res.data
      },
      fail(error) {
        console.log('error')
        console.log(error)
      }
    })
    // const result = await fetch(url, {
    //   method: 'post',
    //   signal: controller.signal,
    //   // TODO 优化
    //   // 开启后到达设定时间会中断流式输出
    //   headers: {
    //     'Content-Type': 'application/json',
    //     Authorization: getTokenValue() || '',
    //   },
    //   body: JSON.stringify(content),
    // })
  } catch (error) {
    throw error
  }
}

export const controllerAbort = () => {
  if (controller) {
    controller.abort()
  }
}